﻿using System.Web.Mvc;
using System.Web.Security;

namespace Nasa8x.Mvc.Helpers.Attributes
{
   public class RequiresAuthenticationAttribute : AuthorizeAttribute
    {
        public override void OnAuthorization(AuthorizationContext _filterContext)
        {
            //redirect if not authenticated
            if (!_filterContext.HttpContext.User.Identity.IsAuthenticated)
            {

                //use the current url for the redirect
                string _redirectOnSuccess = _filterContext.HttpContext.Request.Url.AbsolutePath;
                //send them off to the login page
                string _redirectUrl = string.Format("?ReturnUrl={0}", _redirectOnSuccess);
                string _loginUrl = FormsAuthentication.LoginUrl + _redirectUrl;
                _filterContext.HttpContext.Response.Redirect(_loginUrl, true);

            }

        }
    }
}
